<?php
	include_once("../Business/survey_service.php");
	include_once("../Business/survey_question_service.php");
	include_once("../Business/survey_answer_service.php");
	include_once("../Business/survey_result_service.php");
	include_once("../Business/facebook_app_service.php");
	
	
	include_once("../Business/Data/Entity/survey.php");
	include_once("../Business/Data/Entity/survey_question.php");
	include_once("../Business/Data/Entity/survey_answer.php");
	include_once("../Business/Data/Entity/survey_result.php");
	include_once("../Business/Data/Entity/facebook_app.php");
	
	include_once("uitilities.php");
	
	
	$msgstatus = 'display:none';
	$msg = '';
	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		$ids = split(" ",post("ids"));
		foreach($ids as $id){
			$s = survey_service::get_survey($id);
			survey_service::delete_survey($id);
			if($s->survey_app_id)
				facebook_app_service::delete_facebook_app($s->survey_app_id);
			
		}	
		if(count($ids)>0) {
			$msgstatus = '';
			$msg = 'Delete survey(s) successfully';
		}
	}
	
	$data = '';
	$slist = survey_service::get_survey_list();

	for($i=0 ; $i<count($slist) ; $i++) {
		$s = $slist[$i];
		$sq = survey_question_service::get_survey_question_list_by_survey_id($s->survey_id);
		$sr = survey_result_service::get_survey_result_list_by_survey_id($s->survey_id);
		
		$data = $data.'
		<tr class="'.($i%2==0?"even":"odd").'">
			<td class="checkcol">
				<input type="checkbox" name="cb" onclick="updateIDs()">
				<input type="hidden" name="id" value="'.$s->survey_id.'">
			</td>
			<td>
				<a id="'.$i.'" name="sn" title="Edit" href="edit_survey.php?id='.$s->survey_id.'">'.htmlspecialchars_decode($s->survey_name).'</a>
			</td>
			<td style="text-align:center">
				<a title="See questions" href="'. (count($sq)>0 ? 'survey_question.php?id='.$s->survey_id : '#'). '">'.count($sq).'</a>
			</td>
			<td style="text-align:center">
				<a title="Edit results" href="survey_result.php?id='.$s->survey_id.'">'.count($sr).'</a> 
			</td>
			<td>
				<center>'.($s->survey_app_id ?'<a href="add_facebook_app.php?app_id='.$s->survey_app_id.'&survey_id='.$s->survey_id.'">Facebook App</a> ':'').'
				</center>
			</td>
		</tr>
		';
	}

	$main_content_title = '';
	$main_content = '
	<h3>Survey list</h3>
<form method="post">
<input type="hidden" name="ids" value="">
<p class="label-submit">
	<input value="Delete" type="submit" name="btnDelete" onclick="return confirmDelete(\'survey(s)\');">
</p>
<table class="contentdata"  style="width:100%">
	<thead>
		<tr>
			<th style="width:2%" class="checkcol"><input type="checkbox" name="cb" onclick="checkAll(this)"></th>
			<th style="width:60%">Survey name</th>
			<th style="text-align:center">Questions</th>
			<th style="text-align:center">Results</th>
			<th style="text-align:center">Actions</th>
	</thead>
	<tbody>
		'.$data.'
	</tbody>
        <thead>
		<tr>
			<th style="width:2%" class="checkcol"><input type="checkbox" name="cb" onclick="checkAll(this)"></th>
			<th style="width:60%">Survey name</th>
			<th style="text-align:center">Questions</th>
			<th style="text-align:center">Results</th>
			<th style="text-align:center">Actions</th>
	</thead>
</table>
<p class="label-submit">
	<input value="Delete" type="submit" name="btnDelete" onclick="return confirmDelete(\'survey(s)\');">
</p>
</form>
<script>
	function checkAll(cb) {
		var cbs = $("input[name=cb]")
		cbs.each(function(index) {
			$(this).attr("checked", cb.checked);
		});
	}
	function updateIDs() {
		var ids = "";
		$("input:checked[name=cb]").each(function(index) {
			ids += " " +$(this).next().val();
		});
		$("input[name=ids]").val(ids);
	}
	
	function init() {
		$("a[name=sn]").each(function(index) {			
                var data = htmlspecialchars_decode($(this).html());

                if(data.indexOf("<")>=0) {
			$(this).tooltip({ 
				width: 200,
				track: true,
				delay: 2, 
				showURL: false, 
				bodyHandler: function() { 
					
					return $(data);
				} 
			});
                }
		});
                
	}
	init();
</script>
';
	
	include_once("masters/pages.php");
?>